package gui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;

import javax.swing.AbstractAction;
import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;

import stuff.CloseAction;
import stuff.FixedSizeDocument;
import stuff.Result;

public class GUILogin extends GUIDialog{
	private static final long serialVersionUID = 1L;
	private JTextField tfName, tfPass;
	private JButton btLogin,btCancel, btIPConfig;
	private JLabel loginResult;
	private GUIHome home;
	private JFrame param;
	
	public GUILogin(JFrame param, GUIHome home) {
		super(param);
		this.home = home;
		this.param = param;
		setExitAction(new CloseAction());
		setTitle("Đăng nhập");
		
		JPanel loginPanel = new JPanel(new GridBagLayout());
		loginPanel.setOpaque(false);
		
		JLabel lbName, lbPass;
		lbName = new JLabel();
		lbName.setText("Tên đăng nhập :  ");
		lbName.setFont(lbName.getFont().deriveFont(16f));
		lbName.setForeground(new Color(40,40,40));
		
		lbPass = new JLabel();
		lbPass.setText("Mật khẩu :");
		lbPass.setFont(lbPass.getFont().deriveFont(16f));
		lbPass.setForeground(new Color(40,40,40));
		
		loginResult = new JLabel("");
		loginResult.setFont(lbPass.getFont().deriveFont(12f));
		loginResult.setForeground(Color.red);
		
		tfName = new JTextField();
		tfName.setDocument(new FixedSizeDocument(20));
		tfName.setPreferredSize(new Dimension(0, tfName.getPreferredSize().height +6));
		tfName.setFont(tfName.getFont().deriveFont(16f));
		tfName.setForeground(new Color(40,40,40));
		
		tfPass = new JPasswordField();
		tfPass.setDocument(new FixedSizeDocument(20));
		tfPass.setPreferredSize(new Dimension(0, tfPass.getPreferredSize().height +6));
		tfPass.setFont(tfPass.getFont().deriveFont(16f));
		tfPass.setForeground(new Color(40,40,40));
		
		GridBagConstraints c= new GridBagConstraints();
		c.fill = GridBagConstraints.HORIZONTAL;
		c.gridx = 0;
		c.gridy = 0;
		c.weightx = 0;
		loginPanel.add(lbName,c);
		c.gridx = 1;
		c.weightx = 0.7f;
		loginPanel.add(tfName,c);
		
		c.gridx = 1;
		c.gridy = 1;
		loginPanel.add(Box.createVerticalStrut(6),c);
		
		c.gridx = 0;
		c.gridy = 2;
		c.weightx = 0;
		loginPanel.add(lbPass,c);
		c.gridx = 1;
		c.weightx = 0;
		loginPanel.add(tfPass,c);
		
		c.gridx = 1;
		c.gridy = 3;
		loginPanel.add(Box.createVerticalStrut(6),c);
		
		c.gridx = 0;
		c.gridy = 4;
		c.weightx = 0;
		c.gridwidth = 2;
		loginPanel.add(loginResult,c);
		
		JPanel buttonPanel = new JPanel(new BorderLayout());
		JPanel left, right;
		left = new JPanel(new FlowLayout(FlowLayout.LEFT));
		right = new JPanel(new FlowLayout(FlowLayout.RIGHT));
		left.setOpaque(false);
		right.setOpaque(false);
		loginPanel.setOpaque(true);
		buttonPanel.add(left, BorderLayout.WEST);
		buttonPanel.add(right, BorderLayout.EAST);
		
		btLogin = new CustomJButton("Đăng nhập", 110, 30, Color.WHITE, CustomJButton.DARKER, 
				Color.DARK_GRAY, 2, false, true);
		btLogin.addActionListener(new LoginAction());
		
		btCancel = new CustomJButton("Thoát", 90, 30, Color.WHITE, CustomJButton.DARKER, 
				Color.DARK_GRAY, 2, false, true);
		btCancel.addActionListener(new CloseAction());
		
		btIPConfig = new CustomJButton("!", 30, 30, Color.WHITE, CustomJButton.DARKER, 
				Color.DARK_GRAY, 2, false, true);
		btIPConfig.addActionListener(new ipConfigAction());
		
		left.add(btIPConfig);
		right.add(btLogin);
		right.add(btCancel);
		
		JPanel displayPanel = new JPanel(new BorderLayout());
		displayPanel.setOpaque(false);
		displayPanel.setOpaque(false);
		displayPanel.setBorder(new EmptyBorder(10, 10, 10, 10));
		displayPanel.add(loginPanel,BorderLayout.CENTER);
		displayPanel.add(buttonPanel,BorderLayout.SOUTH);
		
		this.setContentLayout(displayPanel);
	}
	
	class ipConfigAction extends AbstractAction{

		private static final long serialVersionUID = 1L;

		@Override
		public void actionPerformed(ActionEvent e) {
			GUIIPConfig dbConfig = new GUIIPConfig(GUILogin.this);
			dbConfig.pack();
			dbConfig.setSize(350, 180);
			dbConfig.setLocationRelativeTo(GUILogin.this);
			dbConfig.setVisible(true);
		}
	}
	
	class LoginAction extends AbstractAction{
		
		private static final long serialVersionUID = 1L;

		@Override
		public void actionPerformed(ActionEvent e) {
			if(tfName.getText().trim().equals("")){
				loginResult.setText("Tên đăng nhập không được để trống.");
				return;
			}
			if(tfPass.getText().trim().equals("")){
				loginResult.setText("Mật khẩu không được để trống.");
				return;
			}
			Result rs = home.login(tfName.getText().trim(), tfPass.getText());
			if(rs.isRs()){
				param.setEnabled(true);
				param.setVisible(true);
				dispose();
			}else{
				loginResult.setText(rs.getStatus());
			}
		}
	}
}
